package com.ruoyi.archives.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.ruoyi.archives.utile.DirectorRoleUtile;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.UuidUtils;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.archives.mapper.SysArchivessystemMapper;
import com.ruoyi.archives.domain.SysArchivessystem;
import com.ruoyi.archives.service.ISysArchivessystemService;

/**
 * 档案管理Service业务层处理
 * 
 * @author gzhankh
 * @date 2022-02-26
 */
@Service
public class SysArchivessystemServiceImpl implements ISysArchivessystemService 
{
    @Autowired
    private SysArchivessystemMapper sysArchivessystemMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private DirectorRoleUtile directorRoleUtile;
    /**
     * 查询档案管理
     * 
     * @param id 档案管理主键
     * @return 档案管理
     */
    @Override
    public SysArchivessystem selectSysArchivessystemById(String id)
    {
        return sysArchivessystemMapper.selectSysArchivessystemById(id);
    }

    /**
     * 查询档案管理列表
     * 
     * @param sysArchivessystem 档案管理
     * @return 档案管理
     */
    @Override
    public List<SysArchivessystem> selectSysArchivessystemList(SysArchivessystem sysArchivessystem)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        int stDirectorRole = directorRoleUtile.getSTDirectorRole(loginUser);


        List<SysArchivessystem> sysArchivessystems = sysArchivessystemMapper.selectSysArchivessystemList(sysArchivessystem);
        if(!SecurityUtils.isAdmin(loginUser.getUserId())&&stDirectorRole!=0){
            ArrayList<SysArchivessystem> sysArchivesSystemsUser = new ArrayList<>();
            for (SysArchivessystem archivessystem : sysArchivessystems) {
                if(archivessystem.getArcNum().equals(loginUser.getUserId().toString())){
                    sysArchivesSystemsUser.add(archivessystem);
                }
            }
            return sysArchivesSystemsUser;
        }else{
            return sysArchivessystemMapper.selectSysArchivessystemList(sysArchivessystem);
        }
    }

    /**
     * 新增档案管理
     * 
     * @param sysArchivessystem 档案管理
     * @return 结果
     */
    @Override
    public int insertSysArchivessystem(SysArchivessystem sysArchivessystem)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if(sysArchivessystemMapper.selectArchivesByUserName(loginUser.getUsername())!=null){
            throw new ServiceException("一人只允许有一份档案！");
        }else{
            sysArchivessystem.setId(UuidUtils.get8UUID());
            sysArchivessystem.setArcNum(loginUser.getUserId().toString());
            sysArchivessystem.setUserName(loginUser.getUsername());
            Long deptId = loginUser.getDeptId();
            SysDept sysDept = sysDeptMapper.selectDeptById(deptId);
            sysArchivessystem.setProfessional(sysDept.getDeptName());
        }
        return sysArchivessystemMapper.insertSysArchivessystem(sysArchivessystem);
    }

    /**
     * 修改档案管理
     * 
     * @param sysArchivessystem 档案管理
     * @return 结果
     */
    @Override
    public int updateSysArchivessystem(SysArchivessystem sysArchivessystem)
    {
        return sysArchivessystemMapper.updateSysArchivessystem(sysArchivessystem);
    }

    /**
     * 批量删除档案管理
     * 
     * @param ids 需要删除的档案管理主键
     * @return 结果
     */
    @Override
    public int deleteSysArchivessystemByIds(String[] ids)
    {
        return sysArchivessystemMapper.deleteSysArchivessystemByIds(ids);
    }

    /**
     * 删除档案管理信息
     * 
     * @param id 档案管理主键
     * @return 结果
     */
    @Override
    public int deleteSysArchivessystemById(String id)
    {
        return sysArchivessystemMapper.deleteSysArchivessystemById(id);
    }

    @Override
    public List<SysArchivessystem> selectList(SysArchivessystem sysArchivessystem) {
        return sysArchivessystemMapper.selectSysArchivessystemList(sysArchivessystem);
    }
}
